<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
    <title>字段管理</title>
    <%@ include file="../jslib/jquerylib.jsp" %>
    <link rel="stylesheet" type="text/css" href="<%=path %>/web/style/layout.css" />
    <script type="text/javascript" src="<%=path %>/jqGrid/js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="<%=path %>/jslib/jquery.validate.min.js"></script>
    <script type='text/javascript' src="<%=request.getContextPath() %>/jslib/My97DatePicker/WdatePicker.js" defer="defer"></script>
    <script type='text/javascript' src="<%=request.getContextPath() %>/jslib/jquery.form.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath() %>/jslib/additional-methods.js"></script>
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/jslib/jquery-easyui-1.4.1/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/jslib/jquery-easyui-1.4.1/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/jslib/jquery-easyui-1.4.1/demo.css">
	<script type="text/javascript" src="<%=request.getContextPath() %>/jslib/jquery-easyui-1.4.1/jquery.easyui.min.js"></script>
    <script type="text/javascript">
     var api = frameElement.api, W = api.opener;
    	$(function(){
    		$('#closeBtn').click(function(){
				api.close();
			});
    		var options = {
	    	    url:'saveAutoFormField.action',
	    	    beforeSubmit: function() {
	    	    	return checking();
	    		},
	    	    dataType:'json',
	    	    success: function(data) {
	    	      if(data.result=='success'){
					alert("添加成功");
					api.get('cerify').refresh();
					api.close();
	    	      }else{
	    	      	alert("系统繁忙，请您稍后再试！");
	    	      	api.close();
					W.refresh();
	    	      }
    		}};
    		
    		var reoptions = {
	    	    url:'addFromField.action',
	    	    beforeSubmit: function() {
  					return true;
	    		},
	    	    dataType:'json',
	    	    success: function(data) {
	    	      if(data.result=='success'){
					alert("添加成功");
					api.close();
					W.refresh();
	    	      }else{
	    	      	alert("系统繁忙，请您稍后再试！");
	    	      	api.close();
					W.refresh();
	    	      }
    		}};
    		
			/*ajax方式提交表单*/
			$('#entry').click(function() {
		        $('#fom').ajaxSubmit(options);
		        return false; 
		    });
		    
		    /*ajax方式重新提交*/
		    $('#reentry').click(function() {
		        $('#fom').ajaxSubmit(reoptions);
		        return false; 
		    });
		    
		    $(".needhidden").each(function(i){
			   this.style.display="none";
			 });
    	});
    	
    	function selectFieldLength(){
    		var fieldType = $("#fieldType").val();
    		if(fieldType == 'VARCHAR2'){
    			$("#disField").css("display","block");
    		}else{
    			$("#disField").css("display","none");
    		}
    	}
    	
    	  //选择外键表名
		function chooseForeignTableName(){
	     /*var datas = window.showModalDialog("<%=basePath%>/resource/choose/choose_foreign_table_name.jsp?temp="+Math.round(Math.random()*10000),'',"dialogWidth=1000px;dialogHeight=500px");
				if(typeof(datas) == 'undefined'){
				}else{
					callbackForeignTableName(eval('(' + datas + ')'));
				}
		 */
			var height = 500;
			var width = 800;
			var top=Math.round((window.screen.height-height)/2);
			var left=Math.round((window.screen.width-width)/2);
			var url="<%=basePath%>/resource/choose/choose_foreign_table_name.jsp?foreignTableName=foreignTableName&&foreignTableId=foreignTableId&temp="+Math.round(Math.random()*10000);
		    window.open(url,"","width="+width+",height="+height+",top="+top+",left="+left);
		    $("#foreignCloumn").val("");
			$("#foreignDisCloumn").val("");
       }
        //外键表名回调赋值
       function callbackForeignTableName(json){
 			$("#foreignTableName").val(json.form_en_name);
 			$("#foreignTableId").val(json.formId);
 			$("#foreignCloumn").val("");
 			$("#foreignDisCloumn").val("");
	   }
	   
	   //选择外键字段
	   function chooseForeignCloumn(){
	   		var foreignFormId = $("#foreignTableId").val();
	   		if(foreignFormId == ""){
	   			alert("请先选择外键表！");
	   			return false;
	   		}
  			/*var datas = window.showModalDialog("<%=basePath%>/resource/choose/choose_foreign_cloumn.jsp?formId="+foreignFormId+"&temp="+Math.round(Math.random()*10000),'',"dialogWidth=1000px;dialogHeight=500px");
			if(typeof(datas) == 'undefined'){
			}else{
				callbackForeignCloumn(datas);
			}*/
			var height = 500;
			var width = 800;
			var top=Math.round((window.screen.height-height)/2);
			var left=Math.round((window.screen.width-width)/2);
			var url="<%=basePath%>/resource/choose/choose_foreign_cloumn.jsp?foreignCloumn=foreignCloumn&formId="+foreignFormId+"&temp="+Math.round(Math.random()*10000)
		    window.open(url,"","width="+width+",height="+height+",top="+top+",left="+left);
		
	   }
	    //外键表名回调赋值
       function callbackForeignCloumn(datas){
 			$("#foreignCloumn").val(datas);
	   }
	   
	   //选择外键显示字段
	   function chooseForeignDisCloumn(){
	   		var foreignFormId = $("#foreignTableId").val();
	   		if(foreignFormId == ""){
	   			alert("请先选择外键表！");
	   			return false;
	   		}
  			/*var datas = window.showModalDialog("<%=basePath%>/resource/choose/choose_foreign_cloumn2.jsp?formId="+foreignFormId+"&temp="+Math.round(Math.random()*10000),'',"dialogWidth=1000px;dialogHeight=500px");
			if(typeof(datas) == 'undefined'){
			}else{
				callbackForeignDisCloumn(datas);
			}*/
			
			var height = 500;
			var width = 800;
			var top=Math.round((window.screen.height-height)/2);
			var left=Math.round((window.screen.width-width)/2);
			var url="<%=basePath%>/resource/choose/choose_foreign_cloumn2.jsp?foreignDisCloumn=foreignDisCloumn&formId="+foreignFormId+"&temp="+Math.round(Math.random()*10000);
		    window.open(url,"","width="+width+",height="+height+",top="+top+",left="+left);
		   
	   }
	    //外键显示字段回调赋值
       function callbackForeignDisCloumn(datas){
 			$("#foreignDisCloumn").val(datas);
	   }
	   
	     //选择序号生成规则
	   function chooseCustomNo(){
  			var datas = window.showModalDialog("<%=basePath%>/resource/choose/choose_custom_no.jsp?temp="+Math.round(Math.random()*10000),'',"dialogWidth=1000px;dialogHeight=500px");
			if(typeof(datas) == 'undefined'){
			}else{
				callbackCustomNo(eval('(' + datas + ')'));
			}
	   }
	    //序号生成规则回调赋值
       function callbackCustomNo(datas){
 			$("#customNoName").val(datas.customNum);
 			$("#customNoId").val(datas.customId);
	   }
	   
	   function disCustomNo(){
	   		var xial = $("#pageDisplayType").val();
	   		if(xial == "19"){
	   			 $("#customDiv").show(); 
	   		}else{
	   			$("#customDiv").hide(); 
	   		}
	   }
	   //表单校验
	   function checking(){
	   		var fieldName = $("#fieldName").val();
	   		var cnName = $("#cnName").val();
	   		var showSeq = $("#showSeq").val();
	   		var fieldListWidth = $("#fieldListWidth").val();
	   		var foreignTableName = $("#foreignTableName").val();
	   		var foreignCloumn = $("#foreignCloumn").val();
	   		var foreignDisCloumn = $("#foreignDisCloumn").val();
	   		var myPattern = new RegExp("^[a-zA-Z]");
	   		 
	   		var customNoName = $("#customNoName").val();
	   		var pageDisplayType = $("#pageDisplayType").val();
	   		if(cnName ==null || cnName == "" ){
					alert("中文名称不能为空！");
					$("#cnName").focus();
		    		  return false;
			}
			/*else if(!checkExp(cnName)){
				alert("中文名称不能含有除_以外的特殊符号!");
				$("#cnName").focus();
				return false;
	  		}*/
	  		
	  		 var myPattern = new RegExp("^[a-zA-Z]");
	   		if(fieldName ==null || fieldName == "" ){
					alert("字段名称不能为空！");
					$("#fieldName").focus();
		    		  return false;
			}else if(fieldName.indexOf(" ") >=0){
				alert("字段名称不能含有空格！");
				$("#fieldName").focus();
				return false;
			}
			else if(!checkExp(fieldName)){
				alert("字段名称不能含有除_以外的特殊符号!");
				$("#fieldName").focus();
				return false;
	  		}
	  		else if(fieldName.indexOf("-") >=0){
	  			alert("字段名称不能含有除_以外的特殊符号!");
				$("#fieldName").focus();
				return false;
	  		}else if(!myPattern.exec(fieldName)) {
    	       alert("字段名称必须以字母开头!");
    	       $("#fieldName").focus();
    	       return false;
    	    }else if(isChina(fieldName)) {
    	       alert("字段名称不能含有中文!");
    	       $("#fieldName").focus();
    	       return false;
    	    }else if(!checkFieldName()){	
				$("#fieldName").focus();
				 return false;
				flag = false;
			}
	  		//else if(!checkZimu(fieldName)) {
	   	    //   alert("字段名称必须输入字母!");
	   	    //   $("#fieldName").focus();
	   	    //   return false;
	   	   // }
	   	    if(showSeq!=""){
	   	    	if(checkNum(showSeq)){
			    	
			    }else{
			    	alert("显示序列请输入正整数！");
				    $("#showSeq").focus();
				    return false;
			    }
	   	    }
	   	    if(fieldListWidth!=""){
	   	    	if(checkNum(fieldListWidth)){
			    	
			    }else{
			    	alert("列表显示宽度请输入正整数！");
				    $("#fieldListWidth").focus();
				    return false;
			    }
	   	    }
	   	    if(foreignTableName!=null && foreignTableName!=""){
	   	    	if(foreignCloumn==null || foreignCloumn==""){
	   	    		alert("请选择外键字段！");
				    $("#foreignCloumn").focus();
				    return false;
	   	    	}
	   	    	if(foreignDisCloumn==null || foreignDisCloumn==""){
	   	    		alert("请选择外键显示字段！");
				    $("#foreignDisCloumn").focus();
				    return false;
	   	    	}
	   	    } 
	   		if(pageDisplayType==19 && customNoName==""){
	   			alert("请选择序号规则！");
			    $("#customNoName").focus();
			    return false;
	   		} 
	   	    
	   }
	   //验证特殊字符
	function checkExp(inputData){ 
		var regEx = new RegExp(/^(([^\^\.<>%&',:;=?$"'#@!~\]\()[{}【】￥（）！·‘’”“。，、？：；—|\/\\/`\|])*)$/);
		var result = inputData.match(regEx);
		if (result == null) {
		return false;
		}
		return true;
	}
	function checkFieldName(){
   		var flag1 = true;
		$.ajax({
		   type: "POST",
		   async: false,//取消异步提交
		   url: "<%=request.getContextPath() %>/findFieldName.action",
		   dataType: "json",
		   data: {
		   		fieldName: function(){
					return encodeURIComponent($("#fieldName").val());
				},
				formId:<%=request.getParameter("formId")%>
		   },
		   success: function(data){
		   	 if(data.resultes != "success"){
		     	alert( "已存在该字段名称!" );
		     	flag1 = false;
		     }
		   }
		});
		return flag1;
   	}
	function isChina(s){    
			var patrn=/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi;    
			if(!patrn.exec(s)){    
				return false;    
			}else{    
				return true;    
			}    
		}    
	function countlen(textarea){
	    if (textarea.value.length> 250){
	        alert("最多为250个字符,字符输入超出限制!");
	        textarea.value=textarea.value.substring(0,250);
	    }
	    return true;
	}
	//数值校验
	function checkNum(inputData){
		var g = /^[1-9]*[1-9][0-9]*$/;
   		return g.test(inputData);
	}
	//字母校验
	function checkZimu(inputData){
		var z = /^[a-zA-Z]*$/;
		return z.test(inputData);
	}
    </script>
     <style type="text/css">
		body{
			background:#ffffff; background-image:url(images/);
		}
	</style>
  </head>
  
  <body>
    	<form class="fom" id="fom" method="post" action="" >
    	<input type="hidden" name="formId" id="formId" value="<%=request.getParameter("formId")%>"/>
    	<input type="hidden" name="formTableName" id="formTableName" value="<%=request.getParameter("formTableName")%>"/>
            <table border="0" cellpadding="4" cellspacing="1" width="100%" class="formTableNew">
                <tr>
					<td align="right">中文名称:</td>
				    <td>
				    	<input type="text" id="cnName" maxlength="50" name="cnName" value=""/>
				    </td>
				    <td align="right">字段名称:</td>
				    <td>
				    	<input type="text" id="fieldName" maxlength="30" name="fieldName" value=""/>
				    </td>
				</tr>
				<tr>
					<td align="right">字段说明:</td>
				    <td>
				    	<input type="text" id="fieldNote" maxlength="50" name="fieldNote" value=""/>
				    </td>
				    <td align="right">字段类型:</td>
				    <td>
				      <div style="float: left;">
				    	<select id="fieldType" style="width: 100px;" name="fieldType" onchange="selectFieldLength();">
				    		<option value="NUMBER">数值</option>
				    		<option value="VARCHAR2">文本</option>
				    		<option value="DATE">日期</option>
				    	</select>
				    	</div>
				    	<div id="disField" style="float: left;margin-left: 10px; display: none;">
				    	<div style="float:left;margin-top:5px;">长度:</div><div style="float: left;"><input type="text" name="fieldLength" id="fieldLength" style="width: 64px;height:10px;" value="100"/></div>
				    	</div>
				    </td>
				</tr>
				<tr>
					<td align="right">是否只读:</td>
				    <td>
						<select id="isReadOnly" name="isReadOnly" style="width: 212px;">
				    		<option value="0">否</option>
				    		<option value="1">是</option>
				    	</select>
				    </td>
				    <td align="right">验证类型:</td>
				    <td>
				    	<select id="authenticationType" name="authenticationType" style="width: 212px;">
				    		<option value="0">无</option>
				    		<option value="1">必填</option>
				    	</select>
				    </td>
				</tr>
				<tr>
					<td align="right">是否列表显示:</td>
				    <td>
						<select id="isListShow" name="isListShow" style="width: 212px;">
				    		<option value="1">是</option>
				    		<option value="0">否</option>
				    	</select>
				    </td>
				    <td align="right">是否页面显示:</td>
				    <td>
						<select id="isPageShow" name="isPageShow" style="width: 212px;">
				    		<option value="1">是</option>
				    		<option value="0">否</option>
				    	</select>
				    </td>
				</tr>
				<tr>
					<td align="right">是否查询条件:</td>
				    <td>
						<select id="isQueryField" name="isQueryField" style="width: 212px;">
				    		<option value="0">否</option>
				    		<option value="1">是</option>
				    	</select>
				    </td>
				    <td align="right">显示序列:</td>
				    <td>
						<input type="text" id="showSeq" name="showSeq" value=""/>
				    </td>
				</tr>
				<tr>
					<td align="right">是否排序字段:</td>
				    <td>
						<select id="isOrderBy" name="isOrderBy" style="width: 212px;">
				    		<option value="0">否</option>
				    		<option value="1">是</option>
				    	</select>
				    </td>
				    <td align="right">列表显示宽度:</td>
				    <td>
						<input type="text" id="fieldListWidth"  name="fieldListWidth" value="100"/>
				    </td>
				</tr>
				<tr>
					<td align="right">页面显示类型:</td>
				    <td>
						<select id="pageDisplayType" name="pageDisplayType" onchange="disCustomNo();" style="width: 212px;">
				    		<option value="0">文本</option>
				    		<option value="1">时间控件</option>
				    		<option value="2">下拉菜单</option>
				    		<option value="3">文本域</option>
				    		<option value="4">单选框</option>
				    		<option value="5">复选框</option>
				    		<option value="6">默认当前操作人</option>
				    		<option value="7">默认当前操作部门</option>
				    		<option value="8">默认当前操作时间</option>
				    		<option value="9">默认当前操作日期</option>
				    		<option value="10">选择部门</option>
				    		<option value="11">选择人员</option>
				    		<option value="13">文件上传</option>
				    		<option value="19">编号规则自动生成</option>
				    		<option value="20">编号规则选择</option>
				    		<option value="21">根据材料牌号自动变</option>
				    	</select>
				    </td>
				    <td align="right">外键表名:</td>
				    <td>
						<input type="text" id="foreignTableName"  name="foreignTableName" value="" readonly="readonly"/>
						<input type="hidden" id="foreignTableId"  name="foreignTableId" value="" readonly="readonly"/>
						&nbsp;&nbsp;<img src="<%=request.getContextPath() %>/web/images/form_icon_1.gif" width="23" height="23" border="0" onclick="chooseForeignTableName();" title="选择" style="margin-bottom:-6px;cursor:hand;" />
				    </td>
				</tr>
				<tr>
				    <td align="right">外键字段:</td>
				    <td>
						<input type="text" id="foreignCloumn"  name="foreignCloumn" value="" readonly="readonly"/>
						&nbsp;&nbsp;<img src="<%=request.getContextPath() %>/web/images/form_icon_1.gif" width="23" height="23" border="0" onclick="chooseForeignCloumn();" title="选择" style="margin-bottom:-6px;cursor:hand;" />
				    </td>
				    <td align="right">外键显示字段:</td>
				    <td>
						<input type="text" id="foreignDisCloumn"  name="foreignDisCloumn" value="" readonly="readonly"/>
						&nbsp;&nbsp;<img src="<%=request.getContextPath() %>/web/images/form_icon_1.gif" width="23" height="23" border="0" onclick="chooseForeignDisCloumn();" title="选择" style="margin-bottom:-6px;cursor:hand;" />
				    </td>
				</tr>
				<tr>
					<td align="right">默认值:</td>
				    <td>
						<textarea id="defaultValue"  name="defaultValue" rows="1" onkeydown="countlen(this);" style="width: 200px;"></textarea>
				    </td>
				     <td align="left" colspan="2">
					    <div id="customDiv" style="display:none;">
					    	序号规则:
					    	<input type="text" id="customNoName"  name="customNoName" value="" readonly="readonly"/>
					    	<input type="hidden" id="customNoId"  name="customNoId" value="" readonly="readonly"/>
					    	&nbsp;&nbsp;<img src="<%=request.getContextPath() %>/web/images/form_icon_1.gif" width="23" height="23" border="0" onclick="chooseCustomNo();" title="选择" style="margin-bottom:-6px;cursor:hand;" />
				    	</div>
					    </td>
				</tr>
				<tr>
				    <td align="right">事件类型:</td>
				    <td>
				    	<select id="eventType" name="eventType" style="width: 212px;">
				    		<option value=""></option>
				    		<option value="onblur">onblur</option>
				    		<option value="onchange">onchange</option>
				    		<option value="onpropertychange">onpropertychange</option>
				    		<option value="onclick">onclick</option>
				    		<option value="ondblclick">ondblclick</option>
				    		<option value="onfocus">onfocus</option>
				    		<option value="onkeydown">onkeydown</option>
				    		<option value="onkeypress">onkeypress</option>
				    		<option value="onkeyup">onkeyup</option>
				    		<option value="onmousedown">onmousedown</option>
				    		<option value="onmousemove">onmousemove</option>
				    		<option value="onmouseout">onmouseout</option>
				    		<option value="onmouseover">onmouseover</option>
				    		<option value="onmouseup">onmouseup</option>
				    	</select>
				    </td>
				    <td align="right">事件名称:</td>
				    <td>
				    	<input type="text" id="eventName" maxlength="50" name="eventName" value=""/>
				    </td>
				</tr>
				 <tr>
				    <td align="right">列是否合并:</td>
				    <td colspan="3">
				    	<select id="sfhebing" name="sfhebing" style="width: 212px;">
				    		<option value="0">否</option>
				    		<option value="1">是</option>
				    	</select>
				    </td>
				</tr>
				<tr>
				    <td align="right">事件方法:</td>
				    <td colspan="3">
				    	<textarea id="eventFunction"  name="eventFunction" style="width:690px;" rows="2"></textarea>
				    </td>
				</tr>
				
				<tr>
                    <td colspan="4" align="center">
						<input type="button" id="entry" class="form_bt_orange" value="提&nbsp;&nbsp;交" />
						<input type="button" id="closeBtn" class="form_bt_grey" value="取&nbsp;&nbsp;消" />
					</td>
                </tr>
            </table>
        </form>
  </body>
</html>
